<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-09-16 14:24:26
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2022-09-19 13:06:34
 * @FilePath: \ahbs\uc_sys\src\views\UserCenter\Org\components\SetSysRole.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { reactive, ref, unref } from 'vue'
import { LeeEdit } from '@/components/Lee/Edit'
import { ucRoleListApi } from '@/api/uc/role'
import { ucOrgUserListApi } from '@/api/uc/orgUser'
import { required } from '@/utils/formRules'
// // 获取字典
// import { useCache } from '@/hooks/web/useCache'
// const { wsCache } = useCache()
// const dict: any = wsCache.get('dict')
// console.log(dict)
const props = defineProps({
  data: {
    type: Object as any,
    default: () => null
  }
})

const newSysRoleData = ref<any[]>([])

const editSchema = reactive<FormSchema[]>([
  {
    field: 'owner_user_id',
    label: '所有者',
    component: 'DialogRefer',
    componentProps: {
      rules: [required],
      fixedSearch: { org_id: props.data.org_id },
      getListApi: ucOrgUserListApi,
      response: {
        count: 'count',
        data: 'data'
      },
      title: '选择所属员工（双击选择）',
      nameField: 'owner_user_name',
      relNameField: 'nickname',
      valueField: 'user_id',
      crudSchemas: [
        {
          field: 'nickname',
          label: '别名',
          fixed: 'left',
          search: {
            show: true
          }
        },
        {
          field: 'username',
          label: '用户名',
          fixed: 'left',
          search: {
            show: true
          }
        }
      ]
    },
    colProps: {
      span: 24
    }
  },
  {
    field: 'owner_sys_role_id',
    label: '分配系统角色',
    component: 'TreeSelect',
    componentProps: {
      rules: [required],
      data: newSysRoleData,
      'check-strictly': true,
      'default-expand-all': true,
      'node-key': 'role_id',
      props: {
        children: 'children',
        label: 'role_name'
      }
    },
    colProps: {
      span: 24
    }
  }
])

// 获取所有系统角色
const getSysRole = async () => {
  let res = await ucRoleListApi({ is_sys: 1 })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    newSysRoleData.value = res.data
  }
}
// 初始化
const init = async () => {
  // 获取所有系统角色
  await getSysRole()
}
init()

const leeEditRef = ref<ComponentRef<typeof LeeEdit>>()
const elFormRef = () => {
  return unref(leeEditRef)?.elFormRef
}
const getData = async () => {
  return await unref(leeEditRef)?.getFormData()
}
defineExpose({
  elFormRef,
  getData
})
</script>

<template>
  <div>
    <LeeEdit ref="leeEditRef" :schema="editSchema" :data="props.data" />
  </div>
</template>
